package com.hoshiicloud.common.validate.annotations.impl;

import com.hoshiicloud.common.validate.annotations.DateStr;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Markerccc
 * @date 2019/8/28 - 10:22
 */
public class DateStrImpl implements ConstraintValidator<DateStr, String> {

    @Override
    public void initialize(DateStr constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return false;
        }
        Boolean flag = Boolean.TRUE;
        try {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime.parse(value, dateTimeFormatter);
        } catch (Exception e) {
            flag = Boolean.FALSE;
        }
        return flag;
    }

}
